const { getUrlParamsByName } = require('../utils/url-utils')
const { aliasQuery } = require('../utils/graphql-test-utils')
export const loadAllRoutes = () => {
  loadConsoleRoutes()
  loadUserRoutes()
  loadPasswordPolicyRoutes()
  loadAppRoutes()
  loadGraphqlRoutes()
  loadGuardRoutes()
}

export const loadConsoleRoutes = () => {
  cy.intercept({
    method: 'POST',
    pathname: '/api/v2/analysis/user-login-hotdots-by-userpoolIds',
  }).as('userLoginByPoolsIds')

  cy.intercept({
    method: 'GET',
    pathname: '/api/v2/tenant-console-settings',
  }).as('getConsoleSettings')

  cy.intercept({
    method: 'POST',
    pathname: '/api/v2/cost/userpool/package',
  }).as('userpoolPackage')
}

export const loadGraphqlRoutes = () => {
  cy.intercept({ method: 'POST', url: '**/graphql/v2' }, (req) => {
    aliasQuery(req, 'searchUser', { query: 'api-auto-test' })
  })
}

export const loadUserRoutes = () => {
  cy.intercept({
    method: 'POST',
    pathname: '/api/v2/users/filter',
  }).as('userFilter')

  cy.intercept({
    method: 'DELETE',
    pathname: '/api/v2/users/batch',
  }).as('batchDeleteUser')
}

export const loadAppRoutes = () => {
  cy.intercept({
    method: 'POST',
    pathname: '/api/v2/applications',
  }).as('createApp')

  cy.intercept(
    {
      method: 'GET',
      url: '**/api/v2/applications?*',
    },
    (req) => {
      req.alias = getUrlParamsByName(req.url, 'keyword') !== '' ? 'queryAppByKeyword' : 'queryApp'
    }
  )

  cy.intercept({
    method: 'DELETE',
    pathname: '/api/v2/applications/*',
  }).as('deleteApp')

  cy.intercept({
    method: 'POST',
    pathname: '/api/v2/analysis/user-login-hotdots-by-ids',
  }).as('userLoginHotDots')

  cy.intercept({
    method: 'GET',
    url: '**/api/v2/component-page-config-management/guard/app/**',
  }).as('detailApp')
}

export const loadPasswordPolicyRoutes = () => {
  cy.intercept({
    method: 'POST',
    pathname: '/api/v2/password-policy/save',
  }).as('createPolicy')

  cy.intercept({
    method: 'GET',
    pathname: '/api/v2/password-policy/list',
  }).as('queryPolicy')

  cy.intercept({
    method: 'DELETE',
    pathname: '/api/v2/password-policy/*',
  }).as('deletePolicy')
}

export const loadGuardRoutes = () => {
  cy.intercept({
    method: 'GET',
    url: '**/api/v2/guide/get-started?*',
  }).as('guideStarted')

  cy.intercept({
    method: 'POST',
    pathname: '/interaction/authFlow',
  }).as('authFlow')
}
